package com.cgtong.android.mvp.presenter;

import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.bean.SimpleResult;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.http.callback.FollowCallBack;
import com.cgtong.android.http.callback.ProblemDetailCallBack;
import com.cgtong.android.mvp.contract.ProblemDetailContract;
import com.cgtong.android.mvp.model.ProblemDetailModel;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

/**
 * Created by chunc on 2017/10/24.
 */

public class ProblemDetailPresenter implements ProblemDetailContract.Presenter, ProblemDetailCallBack<ProblemBean> {

    private ProblemDetailContract.View mView;
    private ProblemDetailContract.Model mModel;

    public ProblemDetailPresenter(ProblemDetailContract.View mView) {
        this.mView = mView;
        mModel = new ProblemDetailModel();
    }

    @Override
    public void httpProblemDetailData(long problemId) {
        mModel.httpProblemDetailData(problemId, this);
    }

    @Override
    public void onDestroy() {
        if (mView != null) {
            mView = null;
        }
    }

    @Override
    public void follow(final long id) {
        mModel.follow(id, new FollowCallBack<ApiResult>() {
            @Override
            public void onFollowUserSuccess(long id, ApiResult apiResult) {
                if (mView != null){
                    mView.onFollowSuccess(id, apiResult);
                }
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null){
                    mView.onError(e);
                }
            }

        });
    }

    @Override
    public void evaluate(long problemId, int level) {
        if (mView != null) {
            mView.showLoading(true);
        }
        mModel.evaluate(problemId, level, new SimpleCallBack<SimpleResult>() {
            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.showLoading(false);
                    mView.onError(e);
                }
            }

            @Override
            public void onSuccess(SimpleResult simpleResult) {
                if (mView != null) {
                    mView.showLoading(false);
                    mView.onEvaluateSuccess(simpleResult);
                }
            }
        });
    }

    @Override
    public void deleteReport(long id) {
        if (mView != null) {
            mView.showLoading(true);
        }
        mModel.deleteReport(id, new BaseCallBack<ApiResult>() {
            @Override
            public void onSuccess(ApiResult apiResult) {
                if (mView != null){
                    mView.showLoading(false);
                }
                mView.onDeleteReportSuccess(apiResult);
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null){
                    mView.onError(e);
                }
            }

        });
    }

    @Override
    public void onError(ApiException e) {
        if (mView != null) {
            mView.onError(e);
        }
    }

    @Override
    public void onSuccess(ProblemBean problemBean) {
        if (mView != null) {
            mView.onHttpProblemDetailDataSuccess(problemBean);
        }
    }

    @Override
    public void onError(ProblemBean problemBean) {
        if (mView != null) {
            mView.onError(problemBean);
        }
    }
}
